package cn.itrip.order.controller;

import cn.itrip.vo.hotelroom.ItripHotelRoomVO;
import cn.itrip.vo.ItripImageVO;
import cn.itrip.vo.ItripLabelDicVO;
import cn.itrip.dto.Dto;
import cn.itrip.order.service.HotelRoomService;
import cn.itrip.pojo.ItripHotelRoom;
import cn.itrip.utlis.DateUtils;
import cn.itrip.utlis.DtoUtils;
import cn.itrip.utlis.ErrorCode;
import cn.itrip.vo.hotelroom.SearchHotelRoomVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

/**
 * Created by LENOVO on 2018/8/26.
 */
@Controller
@RequestMapping("/api")
public class HotelRoomController {

    @Resource
    private HotelRoomService hotelRoomService;

    /**
     * 根据条件查询酒店房间信息
     * @param searchHotelRoomVO
     * @return
     */
    @ApiOperation(value="查询酒店房间信息",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="查询酒店房间信息")
    @RequestMapping(value = "/hotelroom/queryhotelroombyhotel", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public Dto queryHotelRoombyHotel(@RequestBody SearchHotelRoomVO searchHotelRoomVO) {
        List<List<ItripHotelRoomVO>> list = null;
        List<ItripHotelRoom> itripHotelRoomList = null;
        try {
            if (searchHotelRoomVO != null) {
                Map<String, Object> condition = new HashMap();
                if (searchHotelRoomVO.getStartDate() != null && searchHotelRoomVO.getEndDate() != null) {
                    if (searchHotelRoomVO.getStartDate().getTime() < searchHotelRoomVO.getEndDate().getTime()) {
                        List<Date> dates = DateUtils.getBetweenDates(searchHotelRoomVO.getStartDate(), searchHotelRoomVO.getEndDate());
                        condition.put("timesList", dates);
                    } else {
                        return DtoUtils.returnFail("入住不能大于退房时间", ErrorCode.BIZ_Date_ILLEGAL);
                    }
                } else {
                    return DtoUtils.returnFail("入住及退房时间不能为空", ErrorCode.BIZ_Date_null);
                }
                condition.put("hotelId", searchHotelRoomVO.getHotelId());
                condition.put("isBook", searchHotelRoomVO.getIsBook());
                condition.put("isHavingBreakfast", searchHotelRoomVO.getIsHavingBreakfast());
                condition.put("isTimelyResponse", searchHotelRoomVO.getIsTimelyResponse());
                condition.put("roomBedTypeId", searchHotelRoomVO.getRoomBedTypeId());
                condition.put("isCancel", searchHotelRoomVO.getIsCancel());
                condition.put("payType", searchHotelRoomVO.getPayType());
                itripHotelRoomList = hotelRoomService.HotelRoombyHotel(condition);
                list = new ArrayList<>();
                for (ItripHotelRoom itripHotelRoom : itripHotelRoomList) {
                    List<ItripHotelRoomVO> tempList = new ArrayList<>();
                    ItripHotelRoomVO itripHotelRoomVO = new ItripHotelRoomVO(itripHotelRoom.getId(), itripHotelRoom.getHotelid(), itripHotelRoom.getRoomtitle(), itripHotelRoom.getRoomprice(), itripHotelRoom.getRoombedtypeid(), itripHotelRoom.getIshavingbreakfast(), itripHotelRoom.getPaytype(), itripHotelRoom.getSatisfaction(), itripHotelRoom.getIsbook(), itripHotelRoom.getIscancel(), itripHotelRoom.getIstimelyresponse());
                    tempList.add(itripHotelRoomVO);
                    list.add(tempList);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtils.returnDataSuccess(list);
    }

    /**
     * 根据targetId查找房间图片
     * @param targetId
     * @return
     */
    @ApiOperation(value="查找房间图片",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="查找房间图片")
    @RequestMapping(value = "/hotelroom/getimg/{targetId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<Object> getImgByTargetId(@PathVariable String targetId) {
            List<ItripImageVO> itripImageInfoList = null;
            Map condition = new HashMap();
            condition.put("type", "1");
            condition.put("targetId", targetId);
            try {
                itripImageInfoList = hotelRoomService.getImageListByByTargetId(condition);
                return DtoUtils.returnSuccess("获取酒店图片房型成功", itripImageInfoList);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtils.returnFail("获取酒店房型图片失败", ErrorCode.BIZ_EXCEPTION);
            }
    }

    /**
     *获取酒店床型
     * @return
     */
    @ApiOperation(value="获取酒店床型",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="获取酒店床型")
    @RequestMapping(value = "/hotelroom/queryhotelroombed", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto queryHotelRoomBed() {
        try {
            List<ItripLabelDicVO> itripLabelDicInfoList = hotelRoomService.getHotelRoomBedByParentId(1L);
            return DtoUtils.returnSuccess("获取床型成功", itripLabelDicInfoList);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtils.returnFail("获取床型失败", ErrorCode.BIZ_EXCEPTION);
        }
    }
}
